<template>
  <div>
    <yhqConfig :hasMore="true" :formData.sync="formData"></yhqConfig>
    <div class="bot">
      <el-button @click="back">返 回</el-button>
      <el-button type="primary" @click="addOrEdit">保存并返回</el-button>
    </div>
  </div>
</template>

<script>
import yhqConfig from "@/views/memberManagement/components/yhq-config";
import discountCouponAPI from 'api/marketingManagement/discountCouponAPI';
import {mapState, mapMutations} from 'vuex';

export default {
  components: {
    yhqConfig
  },
  computed: {
    ...mapState('middleData', ['discountCouponObj']),
  },
  data() {
    return {
      formData: {}
    }
  },
  methods: {
    ...mapMutations('middleData', ['setDiscountCouponObj']),
    async addOrEdit() {
      let type = 'add'
      if(this.formData.coupon_id) {
        type = 'edit'
      }
      let data = {...this.formData}
      data.start_time = ~~ (data.start_time / 1000)
      data.end_time = ~~ (data.end_time / 1000)
      data.full_money *= 100 
      data.reduction_money *= 100
      await discountCouponAPI(type, data)
      this.$message.success('操作成功')
      this.back()
    },
    back() {
      this.$router.back()
    }
  },
  beforeDestroy() {
    this.setDiscountCouponObj({})
  },
  created() {
    if(Object.keys(this.discountCouponObj).length) {
      this.formData = {...this.discountCouponObj}
      this.formData.start_time *= 1000
      this.formData.end_time *= 1000
      this.formData.is_core += ''
    }
  }
}
</script>

<style lang="scss" scoped>
.bot{
  display: flex;
  justify-content: flex-end
}
</style>